package org.openclinic.controller;

import javax.servlet.http.HttpServletRequest;

import org.joda.time.LocalTime;
import org.openclinic.domain.Clinic;
import org.openclinic.exception.AbstractExceptionHandler;
import org.openclinic.service.AppointmentService;
import org.openclinic.service.MedicalSpecialtyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WelcomeController extends AbstractExceptionHandler {

	@Autowired
	private Clinic clinic;
	
	@Autowired
	private AppointmentService appointmentService;
	
	@Autowired
	private MedicalSpecialtyService medicalSpecialtyService;
	
	@RequestMapping(value = "/index.do", method = RequestMethod.GET)
	public ModelAndView initCustomer(HttpServletRequest request) {
		request.getSession().invalidate();
		
		LocalTime endTime = new LocalTime(clinic.getAfternoonEndTime());
		ModelAndView mav = new ModelAndView("index.view");
		mav.addObject("startHour", new LocalTime(clinic.getMorningStartTime()).getHourOfDay());
		mav.addObject("endHour", endTime.getMinuteOfHour() == 0 ? endTime.getHourOfDay() - 1 : endTime.getHourOfDay());
		mav.addObject("clinic", clinic);
		mav.addObject("specialtyList", medicalSpecialtyService.findAvailable());
		
		return mav;
	}
	
}
